The var
keyword in PHP was historically used to declare class properties with default public visibility. However, its usage is
discouraged as it lacks clarity and explicit visibility declaration.
Instead, PHP introduced the keywords public
, protected
, and private
to clearly define the visibility of
class properties. This enhances code readability and maintainability, as it becomes easier to understand and control access to class members.
Additionally, using the keywords for explicit visibility helps prevent unintended modifications or security vulnerabilities that may arise from the
ambiguity of the var
keyword.
Noncompliant code example
<?php
class Foo
{
var $bar = 1;
}
Compliant solution
<?php
class Foo
{
public $bar = 1;
}